package com.dyylearn.servlet.annotation;

import com.sun.net.httpserver.HttpServer;

import javax.servlet.annotation.WebServlet;
import java.lang.annotation.Annotation;
import java.util.HashMap;

/**
 * 模拟一把Tomcat是如何通过 @WebServlet(urlPatterns = {"/ok1", "/ok2"})
 * 来装载一个Servlet的
 */
public class TestAnnotationServlet {
    private static final HashMap<String, Object> hm = new HashMap<>();

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        //1.首先要得到扫描的包 路径 id，进而得到类的全路径
        String classAllPath = "com.dyylearn.servlet.annotation.OkServlet";
        //2.得到 OkServlet的Class对象
        Class<?> aClass = Class.forName(classAllPath);
        //3.通过class对象，得到Annotation
        WebServlet annotation = aClass.getAnnotation(WebServlet.class);
        System.out.println(annotation);
        String[] strings = annotation.urlPatterns();
        for(String url : strings) {
            System.out.println("url= " + url);
        }

        //如果匹配url，如果是第一次，tomcat就会创建一个OkServlet实例，放入到 hashmap
        Object instance = aClass.newInstance();
        System.out.println("instance= " + instance);//OkServlet

        //简单地模拟，没有深入
        hm.put("OkServlet", instance);
        System.out.println(hm);
    }
}
